/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.Player;
import models.Ship;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.text.NumberFormat;

/**
 * PrettyMarketPanel is the view of the market. It lets the player buy and sell
 * goods.
 * @author bradley
 * @version 1.0
 */
public class PrettyMarketPanel extends JPanel {
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * field
	 */
	private final Player player;
    
	/**
	 * field
	 */
	private final JLabel shipCapacityField;
    
	/**
	 * field
	 */
	private final JLabel playerCreditsField;
    
	/**
	 * field
	 */
	private final JScrollPane tradeGoodTableScroller; 
    
	/**
	 * field
	 */
	private final TradeGoodTablePanel tradeGoodTable;

    /**
     * Creates a new pretty market panel
     * @param player - the player
     */
    public PrettyMarketPanel(Player player) {
        super.setLayout(null);

        this.player = player; 

        shipCapacityField = new JLabel();
        playerCreditsField = new JLabel();

        tradeGoodTable = new TradeGoodTablePanel(player);
        tradeGoodTableScroller = new JScrollPane(tradeGoodTable);         

        super.add(shipCapacityField);
        super.add(playerCreditsField);
        super.add(tradeGoodTable);
        super.add(tradeGoodTableScroller);

        update();
    }

    /**
     * Updates the panel
     */
    public final void update() {
        tradeGoodTable.update(); 
        final NumberFormat fmt = NumberFormat.getCurrencyInstance();
        final Ship ship = player.getShip(); 

        shipCapacityField.setText("Cargo Slots Remaining: " 
        + ship.getRemainingCargoCapacity());
        playerCreditsField.setText("Credits: " + fmt.format(player.getCredits()));
    }

    /**
     * the start of the capacity field on the x axis
     */
    private static final int CAPACITYX = 10;
    
    /**
     * the width of the capacity field.
     */
    private static final int CAPACITYWIDTH = 20;
    
    /**
     * the height of the capacity field.
     */
    private static final int CAPACITYHEIGHT = 40;
    
    /**
     * the start of the credits field on the x axis
     */
    private static final int CREDITSX = 135;
    
    /**
     * the width of the credits field
     */
    private static final int CREDITSWIDTH = 20;
    
    /** 
     * the height of the credits field
     */
    private static final int CREDITSHEIGHT = 40;
    
    /**
     * scroller x
     */
    private static final int SCROLLERX = 10;
    
    /**
     * scroller y
     */
    private static final int SCROLLERY = 50;
    
    /** 
     * scroller width
     */
    private static final int SCROLLERWIDTH = 20;
    
    /**
     * scroller height
     */
    private static final int SCROLLERHEIGHT = 60;
    
    /**
     * The preferred size x
     */
    private static final int PREFERREDX = 20;
    
    /**
     * the preferred size y
     */
    private static final int PREFERREDY = 40;
    
    /**
     * table x
     */
    private static final int TABLEX = 10;
    
    /**
     * table y
     */
    private static final int TABLEY = 50;
    
    /** 
     * table width
     */
    private static final int TABLEWIDTH = 20;
    
    /**
     * table height
     */
    private static final int TABLEHEIGHT = 360;
    
    /**
     * Sets the layout subviews
     */
    public void layoutSubviews() {
        final Dimension size = getSize();

        shipCapacityField.setBounds(CAPACITYX, 0, 
        		(size.width >> 1) - CAPACITYWIDTH, CAPACITYHEIGHT);
        playerCreditsField.setBounds(size.width - CREDITSX, 
        		0, (size.width >> 1) - CREDITSWIDTH, CREDITSHEIGHT);

        tradeGoodTableScroller.setBounds(SCROLLERX, SCROLLERY, 
        		size.width - SCROLLERWIDTH, size.height - SCROLLERHEIGHT);
        final int rows = tradeGoodTable.getNumTradeGoods();
        tradeGoodTable.setPreferredSize(
        		new Dimension(size.width - PREFERREDX, PREFERREDY * rows));
        tradeGoodTable.setBounds(TABLEX, TABLEY, size.width - TABLEWIDTH, TABLEHEIGHT);
    }

    /**
     * Use to set the size of the component
     * @param x
     * @param y
     * @param width
     * @param height
     */
    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        layoutSubviews();
    }
}
